package com.uenpay.oem.ui.user.welcome;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.uenpay.im.IMApplication;
import com.uenpay.im.view.CirclePageIndicator;
import com.uenpay.oem.R;
import com.uenpay.oem.ui.user.login.LoginActivity;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 欢迎、引导页面
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
@Route(path = "/main/welcome")
public class WelcomeActivity extends BaseActivity {

    private ArrayList<View> views;

    @BindView(R.id.pager)
    ViewPager viewPager;
    @BindView(R.id.indicator)
    CirclePageIndicator circlePageIndicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        isHideStatuBar = true;
        super.onCreate(savedInstanceState);
        IMApplication.setFirstTime();
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initView(View view) {
        views = new ArrayList<>();
        LayoutInflater mLi = LayoutInflater.from(this);
       // views.add(mLi.inflate(R.layout.view_welcome_first, null));
       // views.add(mLi.inflate(R.layout.view_welcome_second, null));
        View clickView = mLi.inflate(R.layout.view_welcome_third, null);
        views.add(clickView);
        clickView.findViewById(R.id.startImageView).setOnClickListener(
                new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LoginActivity.launch(WelcomeActivity.this);
                        finish();
                    }
                });
        viewPager.setAdapter(new ViewPagerAdapter());
        circlePageIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
        circlePageIndicator.setViewPager(viewPager);
        circlePageIndicator.setRadius(getResources().getDimension(
                R.dimen.circle_page_radius_small_width));
        circlePageIndicator.setStrokeColor(getResources().getColor(
                R.color.page_normal_color));
        circlePageIndicator.setFillColor(getResources().getColor(
                R.color.page_select_color));
        circlePageIndicator.setPageColor(getResources().getColor(
                R.color.page_normal_color));
    }

    class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(views.get(position));
            return views.get(position);

        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return (arg0 == arg1);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    public static void launch(Activity activity) {
        ARouter.getInstance().build("/main/welcome").navigation();
    }

}
